﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Project_ScansiaPacific
{
    public partial class FrDangNhap : DevComponents.DotNetBar.Office2007Form
    {
        public FrDangNhap()
        {
            InitializeComponent();
        }

        private void bt_logon_Click(object sender, EventArgs e)
        {
            Data dta = new Data();
            string sql = @"select UsersName,Password from Users";
            //if (dta.check_input(sql, txt_UserName.Text, 0) && dta.check_input(sql, txt_Password.Text, 1))
            SqlConnection Conn = dta.Connect();
            SqlCommand cmd = new SqlCommand();
            try
            {
                Conn.Open();
                cmd.Connection = Conn;
                cmd.CommandText = sql;
                SqlDataReader rd = cmd.ExecuteReader();
                int i = 0;
                while (rd.Read())
                {
                    if (txt_UserName.Text == rd[0].ToString() && txt_Password.Text == rd[1].ToString())
                    {
                        MessageBox.Show("ĐĂNG NHẬP THÀNH CÔNG");
                        FrMain FrM = this.MdiParent as FrMain;
                        FrM.Show();
                        FrM.User(txt_UserName.Text);
                        FrM.Enable(txt_UserName.Text);
                        if (txt_UserName.Text == "HUYNHVU")
                        {
                            dta.backup();
                        }
                        rd.Close();
                        string date = @"select convert(char(10),getdate(),101)";
                        cmd.CommandText = date;
                        date = cmd.ExecuteScalar().ToString();
                        //MessageBox.Show(date);
                        string update = @"UPDATE [dbo].[Users] SET DateAccess='" + date + "' where UsersName='" + txt_UserName.Text + "'";
                        cmd.CommandText = update;
                        cmd.ExecuteNonQuery();
                        i = 0;
                        break;
                    }
                    else
                        i++;
                }
                if (i > 0)
                {
                    MessageBox.Show("Thông tin không chính xác");
                    txt_UserName.Focus();
                }
                else
                    this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }

        private void FrDangNhap_KeyDown(object sender, KeyEventArgs ex)
        {
            if(ex.KeyData==Keys.Enter)
                SendKeys.Send("{TAB}");
        }
    }
}
